<template>
	<view>
		<view class="top-wrap">
			<view class="top">
				<view class="date-wrap">
					<image class="calendar-icon" src="@/static/imgs/icons/calendar1-icon.png" mode="aspectFill"></image>
					<text class="date">{{travelDate}}</text>
				</view>
				<view class="person-wrap">
					<uni-icons type="person-filled" size="24" color="#CCCCCC"></uni-icons>
					<text class="num">{{travelInfo.personNum}}人</text>
				</view>
			</view>
			<view class="address">
				{{travelInfo.fromArea}} — {{travelInfo.toArea}}
			</view>
		</view>

		<view class="left_navigation-wrap">
			<scroll-view class="l-wrap" scroll-y="true">
				<view class="l_item" :class="{active: curNav===it_l}" v-for="(it_l, in_l) in navList" :key="in_l" @click="handleChooseCharteredType(it_l, in_l)">
					{{it_l}}
				</view>
			</scroll-view>

			<scroll-view class="r-wrap" scroll-y="true">
				<view class="list">
					<view class="item" v-for="(item, index) in list" :key="index" @click="handleGoCharteredOrder(item)">
						<image class="img" :src="/^[http | https]/.test(item.img) ? item.img : (wwwImgUrl + item.img)" mode="aspectFill"></image>
						<view class="info">
							<view class="name">{{item.carName}}</view>
							<view class="detail">
								<view class="price">￥{{item.price}} <text style="font-size: 24rpx;font-weight: normal;">/公里</text></view>
								<view class="passenger_num">载客人数:{{item.number}}</view>
							</view>
							<view class="tp-fz24 tp-mt10" style="color: #FFAF3C;">起步价{{item.startPrice || 0}}元（含{{item.startMile || 0}}公里）</view>
						</view>
					</view>
				</view>

				<!-- 不符合车辆展示S -->
				<view class="un_tit" v-if="listNot.length !==0 ">乘车人数已超出以下车辆核载人数</view>
				<view class="list">
					<view class="item un_item" v-for="(item, index) in listNot" :key="index">
						<image class="img" :src="/^[http | https]/.test(item.img) ? item.img : (wwwImgUrl + item.img)"  mode="aspectFill"></image>
						<view class="info">
							<view class="name">{{item.carName}}</view>
							<view class="detail">
								<view class="price">￥{{item.price}} <text style="font-size: 24rpx;font-weight: normal;">/公里</text></view>
								<view class="passenger_num">载客人数:{{item.number}}</view>
							</view>
							<view class="tp-fz24 tp-mt10" style="color: #FFAF3C;">起步价{{item.startPrice || 0}}元（含{{item.startMile || 0}}公里）</view>
						</view>
					</view>
				</view>
				<!-- 不符合车辆展示E -->

				<view class="nodata" v-if="!listNot.length && !list.length">这里还木有数据哈~~</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	var QQMapWX = require('@/utils/qqmap-wx-jssdk/qqmap-wx-jssdk.min.js');
	var qqmapsdk;
	export default {
		data() {
			return {
				navList: [],
				curNav: '经济型',
				travelInfo: {}, // 顶部信息展示
				totalList: [], // 总列表
				list: [], // 符合条件的列表
				listNot: [], // 不符合条件的列表
				cityCross: '0', // 是否跨市 跨域 0.是 1否
			};
		},
		onLoad(opt) {
			this.travelInfo = opt && opt.travelInfo && JSON.parse(opt.travelInfo)
			this.initMap()
		},
		computed: {
			travelDate() {
				let month = this.travelInfo.dateObj && this.travelInfo.dateObj.month
				let day = this.travelInfo.dateObj && this.travelInfo.dateObj.day
				let hour = this.travelInfo.dateObj && this.travelInfo.dateObj.hour && this.travelInfo.dateObj.hour.value
				let minute = this.travelInfo.dateObj && this.travelInfo.dateObj.minute && this.travelInfo.dateObj.minute.value ||
					'00'
				return month + '月' + day + '日' + ' ' + hour + ':' + minute
			},
		},
		methods: {
			initMap() {
				qqmapsdk = new QQMapWX({
					key: 'C7JBZ-VQD6D-BVZ4C-PHTJO-HNPSF-ORFT5'
				});
				this.judgeCross(this.travelInfo.fromObj, this.travelInfo.toObj)

			},
			handleChooseCharteredType(item, i) {
				this.curNav = item
				this.list = this.listNot = []
				let arr = this.totalList[i].list
				let a = []
				let b = []
				arr.forEach(item => {
					if (item.type == 1) {
						a.push(item)
						this.list = a
					} else if (item.type == 2) {
						b.push(item)
						this.listNot = b
					}
				})
			},
			handleGoCharteredOrder(carInfo) {
				this.travelInfo.travelDate = this.travelDate
				uni.navigateTo({
					url: '../pages/order/chartered.order?travelInfo=' + JSON.stringify(this.travelInfo) + '&carInfo=' + JSON.stringify(
						carInfo)
				});
			},
			getList() {
				this.$http.get(this.$api.getCharteredList + this.travelInfo.personNum + '&crop=' + this.cityCross + '&city=' + this.travelInfo.city).then(res => {
					if (res.code === 200) {
						this.list = this.listNot = []
						this.curNav = res.data[0].typeName
						this.totalList = res.data

						res.data.forEach(item => { // 获取车类型列表
							this.navList.push(item.typeName)
						})

						let arr = res.data[0].list
						let a = []
						let b = []
						arr.forEach(it_ => {
							if (it_.type == 1) {
								a.push(it_)
								this.list = a
							} else if (it_.type == 2) {
								b.push(it_)
								this.listNot = b
							}
						})
					}
				})
			},
			async judgeCross(fromObj, toObj) { // 判断是否跨市区  跨域0   没有跨域1
				let _this = this
				// #ifdef MP-WEIXIN
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: fromObj.lat,
						longitude: fromObj.lng
					},
					success: function(res_from) {
						qqmapsdk.reverseGeocoder({
							location: {
								latitude: toObj.lat,
								longitude: toObj.lng
							},
							success: function(res_to) {
								let city_to = res_to.result.address_component.city
								let city_from = res_from.result.address_component.city
								_this.cityCross = city_to == city_from ? '1' : '0'
								_this.getList()
							},
							fail: function(res) {
								_this.getList()
							},
						});
					},
					fail: function(res) {
						_this.getList()
					},
				});
				// #endif
				
				// #ifdef H5
				let data = await _this.exchangeDingwei(fromObj.lat, fromObj.lng)
				if(data){
					let data1 = await _this.exchangeDingwei(toObj.lat, toObj.lng)
					if(data1){
						let city_to = data1.result.address_component.city
						let city_from = data.result.address_component.city
						_this.cityCross = city_to == city_from ? '1' : '0'
						_this.getList()
					}
				}
				// #endif
			}
		}
	}
</script>

<style lang="scss" scoped>
	.top-wrap {
		border-bottom: 1px solid #ebebeb;
		display: flex;
		justify-content: space-evenly;
		flex-direction: column;
		font-size: 24rpx;
		height: 100rpx;
		padding: 0 40rpx 12rpx;
		background-color: #fff;
		position: fixed;
		/* #ifdef MP-WEIXIN */
		top: 0;
		/* #endif */
		/* #ifdef H5 */
		top: 88rpx;
		/* #endif */
		left: 0;
		right: 0;
		z-index: 3;

		.top {
			display: flex;
			align-items: center;

			.date-wrap {
				display: flex;
				align-items: center;

				.calendar-icon {
					width: 32rpx;
					height: 32rpx;
					margin-right: 12rpx;
				}
			}

			.person-wrap {
				margin-left: 62rpx;
			}
		}
	}

	.left_navigation-wrap {
		padding-top: 112rpx;
		display: flex;
		justify-content: space-between;
		position: relative;

		.l-wrap {
			width: 200rpx;
			position: fixed;
			/* #ifdef MP-WEIXIN */
			top: 114rpx;
			/* #endif */
			/* #ifdef H5 */
			top: 202rpx;
			/* #endif */
			left: 0;
			bottom: 0;
			z-index: 3;

			.l_item {
				height: 100rpx;
				width: 200rpx;
				display: flex;
				align-items: center;
				justify-content: space-evenly;
				font-size: 32rpx;
				font-weight: bold;

				&.active {
					background-color: #fff;
					position: relative;

					&::after {
						content: '';
						width: 8rpx;
						height: 42rpx;
						border-radius: 4rpx;
						background-color: $blue;
						position: absolute;
						top: 50%;
						transform: translateY(-50%);
						left: 0;
					}
				}
			}
		}

		.r-wrap {
			width: 550rpx;
			position: fixed;
			/* #ifdef MP-WEIXIN */
			top: 114rpx;
			/* #endif */
			/* #ifdef H5 */
			top: 202rpx;
			/* #endif */
			right: 0;
			bottom: 0;
			flex: 1;
			background-color: #fff;

			.un_tit {
				height: 56rpx;
				line-height: 56rpx;
				text-align: center;
				background-color: #FFF2F1;
				font-size: 22rpx;
				color: $red;
			}

			.list {
				display: flex;
				flex-direction: column;

				.item {
					display: flex;
					justify-content: space-between;
					align-items: center;
					height: 160rpx;
					padding: 0 30rpx;
					border-bottom: 1px solid #ebebeb;

					&:last-child {
						border: none;
					}

					&.un_item {
						background-color: #eaeaea;
						border-bottom: 1px solid #fff;

						&:last-child {
							border: none;
						}
					}

					.img {
						width: 156rpx;
						height: 89rpx;
						background-color: pink;
						margin-right: 36rpx;
					}

					.info {
						flex: 1;

						.name {
							font-size: 30rpx;
						}

						.detail {
							margin-top: 17rpx;
							display: flex;
							justify-content: space-between;
							align-items: center;

							.price {
								font-size: 36rpx;
								color: $red;
								font-weight: bold;
							}

							.passenger_num {
								font-size: 24rpx;
								color: #666;
							}
						}
					}
				}
			}
		}
	}

	.nodata {
		text-align: center;
		color: #999;
		margin: 100rpx auto;
	}
</style>
